package com.hou.controller;

import com.hou.dao.UserDao;
import com.hou.pojo.Result;
import com.hou.pojo.User;
import com.hou.service.UserService;
import com.hou.utils.CreateImageCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Base64Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author shkstart
 * @create 2021-09-18-15:32
 */
@RestController
@RequestMapping("user")
@CrossOrigin
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate redis;

    //用户注册
    @PostMapping("register")
    @ResponseBody
    public Result register(String code, String key, @RequestBody User user){
        Result result = new Result();
        String redisCode = (String) redis.opsForValue().get("code"+key);
        try {
            if (userService.findByUserName(user)){
                if (code.equals(redisCode)){
                    userService.register(user);
                    result.setMsg("注册成功！");
                } else {
                    result.setStatue(false);
                    throw new RuntimeException("验证码错误");
                }
            } else {
                result.setMsg("用户已被注册!");
                result.setStatue(false);
            }

        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg(e.getMessage()).setStatue(false);
        }
        return result;
    }

    //用户登录
    @RequestMapping("login")
    public Result login(@RequestBody User user){
        Result result = new Result();
        String password = user.getPassword();
        User user_data = userService.userInfo(user);
        try {
            boolean equals = password.equals(user_data.getPassword());
            if (equals) {
                result.setStatue(true);
                result.setMsg("登录成功");
            } else {
                result.setStatue(false);
                result.setMsg("登录失败");
            }
        } catch (Exception e){
            result.setMsg("该用户未被注册");
            result.setStatue(false);
        }
        return result;
    }

    //生成验证码
    @GetMapping("getImage")
    @ResponseBody
    public Map<String,String> getImage(HttpServletResponse response) throws IOException {
        Map<String,String> result = new HashMap<>();
        CreateImageCode createImageCode = new CreateImageCode();
        //获取验证码
        String securityCode = createImageCode.getCode();
        //生成注册用户唯一标识
        String key = new SimpleDateFormat("yyyyMMDDHHmmss").format(new Date());
        //验证码存入redis
        redis.opsForValue().set("code"+key,securityCode,120,TimeUnit.SECONDS);
        //生成图片
        BufferedImage image = createImageCode.getBuffImg();
        //base64编码图片
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageIO.write(image,"png",bos);
        String picture = Base64Utils.encodeToString(bos.toByteArray());
        //插入数据
        result.put("picture",picture);
        result.put("key",key);
        return result;
    }

}
